package org.dromara.module.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.dromara.module.domain.AdBanner;
import org.dromara.module.mapper.AdBannerMapper;
import org.dromara.module.service.IAdBannerService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 轮播图（广告）Service业务层处理
 *
 * @author Mr.Zhou
 * @date 2025-01-17
 */
@RequiredArgsConstructor
@Service
public class AdBannerServiceImpl extends ServiceImpl<AdBannerMapper, AdBanner> implements IAdBannerService {

    @Resource
    private AdBannerMapper adBannerMapper;

    @Value("${ruoyi.imgUrl}")
    private String imgUrl;//图片地址

    @Resource
    protected RedisTemplate<String, Object> redisTemplate;

    @Override
    public AdBanner getBannerByAdType(Integer ad_type) {
        AdBanner banner = adBannerMapper.selectOneByColumn("ad_type",ad_type);
        if(banner!=null ){
            String img_key = banner.getImgUrl()!=null ? banner.getImgUrl():"";
            if(!img_key.equals("")){
                banner.setImgUrl(imgUrl + img_key);
            }
        }
        return banner;
    }


    @Override
    public List<AdBanner> getList(int ad_type) {
        List<AdBanner> banners = new ArrayList<>();
        try{
            QueryWrapper<AdBanner> qw = new QueryWrapper<>();
            qw.eq("state",0);
            qw.eq("ad_type",ad_type);
            qw.orderByAsc("sort");

            String key = "ad_banner_" + ad_type;
//            boolean blHasKey = redisTemplate.opsForHash().hasKey(key,key);
//            if (blHasKey) {
//                Object object = redisTemplate.opsForHash().get(key, key);
//                String itemDicts = object != null ? object.toString() : "";
//                if (StrUtil.isNotEmpty(itemDicts)) {
//                    JSONArray jsonArray = JSONUtil.parseArray(itemDicts);
//                    banners = JSONUtil.toList(JSONUtil.toJsonStr(jsonArray), AdBanner.class);
//                } else {
//                    banners = adBannerMapper.selectList(qw);
//                    if(CollUtil.isNotEmpty(banners)){
//                        banners.forEach(banner->{
//                            if(StrUtil.isNotEmpty(banner.getImgUrl())){
//                                banner.setImgUrl(imgPrefix + banner.getImgUrl());
//                            }
//                        });
//                    }
//                    redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(banners));
//                }
//            }else{
//                banners = adBannerMapper.selectList(qw);
//                if(CollUtil.isNotEmpty(banners)){
//                    banners.forEach(banner->{
//                        if(StrUtil.isNotEmpty(banner.getImgUrl())){
//                            banner.setImgUrl(imgPrefix + banner.getImgUrl());
//                        }
//                    });
//                }
//                redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(banners));
//            }

            banners = adBannerMapper.selectList(qw);
            if(CollUtil.isNotEmpty(banners)){
                banners.forEach(banner->{
                    if(StrUtil.isNotEmpty(banner.getImgUrl())){
                        banner.setImgUrl(imgUrl + banner.getImgUrl());
                    }
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return banners;
    }

    @Override
    public void resetBannerData(int ad_type) {
        List<AdBanner> banners = new ArrayList<>();
        try{
            QueryWrapper<AdBanner> qw = new QueryWrapper<>();
            qw.eq("state",0);
            qw.eq("ad_type",ad_type);
            qw.orderByAsc("sort");
            String key = "ad_banner_" + ad_type;
            banners = adBannerMapper.selectList(qw);
            if(CollUtil.isNotEmpty(banners)){
                banners.forEach(banner->{
                    if(StrUtil.isNotEmpty(banner.getImgUrl())){
                        banner.setImgUrl(imgUrl + banner.getImgUrl());
                    }
                });
            }
            redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(banners));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
